<?php
namespace Home\Controller;
use Think\Controller;
// 开发者：杨晓杰，
// 邮箱：2902468225@qq.com 


// 竞赛控制器，负责竞赛相关操作
class CompetitionController extends Controller {
	private $dbName="competition";
	private $dbName2="cpt_sign";
	private $dbName3="competition_result";
	// 竞赛列表
	public function listPage(){
		tag('func');
		$key=I('get.key');
		$level=I('get.level');//1院级，2校级，3省级，4国家级
		$type=I('get.content_type');//内容类型

		$where['tswc_competition.is_verify']=1;
		// 根据标题，内容，描述查找竞赛
		if($key)$where['title|contain|desc']=array('like',"%".$key."%");
		//根据级别查找竞赛
		if($level)$where['level']=$level;
		//根据内容类别查找竞赛
		if($type)$where['content_type']=$type;

		$all=M($this->dbName)->join('__SCHOOL_ORG__ ON __COMPETITION__.school_id = __SCHOOL_ORG__.id')->where($where)->order('cpt_id desc')->field('cpt_id,title,tswc_competition.desc,logo,start_time,end_time,school,is_sign_start')->select();
		$data=R('Myfunc/Myfunc/pageDeal',array('data'=>$all));

		$data['key']=$key;
		$data['lvl']=$level;
		$data['type']=$type;

		$data['level']=array(1=>'国家级',2=>'省级',3=>'校级',4=>'院级');
		$data['content_type']=M('cpt_type')->order('hot desc')->limit(5)->select();
		
		$this->assign('data',$data);
		// dump($data);
        R('Page/cptList');
	}
	public function searchList(){
		tag('func');
		$key=I('get.key');
		$level=I('get.level');//1院级，2校级，3省级，4国家级
		$type=I('get.content_type');//内容类型

		$where['tswc_competition.is_verify']=1;
		// 根据标题，内容，描述查找竞赛
		if($key)$where['title|contain|tswc_competition.desc']=array('like',"%".$key."%");
		//根据级别查找竞赛
		if($level)$where['level']=$level;
		//根据内容类别查找竞赛
		if($type)$where['content_type']=$type;

		$all=M($this->dbName)->join('__SCHOOL_ORG__ ON __COMPETITION__.school_id = __SCHOOL_ORG__.id')->where($where)->order('cpt_id desc')->field('cpt_id,title,tswc_competition.desc,logo,start_time,end_time,school,is_sign_start')->select();
		$data=R('Myfunc/Myfunc/pageDeal',array('data'=>$all));

		$data['key']=$key;
		$data['lvl']=$level;
		$data['type']=$type;

		$data['level']=array(1=>'国家级',2=>'省级',3=>'校级',4=>'院级');
		$data['content_type']=M('cpt_type')->order('hot desc')->limit(5)->select();
		
		$this->assign('data',$data);
		// dump($data);
        R('Page/cptSearchList');
	}
	// 竞赛列表ajax接口
	public function listAjax(){
		tag('func');
		$key=I('get.key');
		$level=I('get.level');//1院级，2校级，3省级，4国家级
		$type=I('get.content_type');//内容类型

		$where['tswc_competition.is_verify']=1;
		// 根据标题，内容，描述查找竞赛
		if($key)$where['title|contain|desc']=array('like',"%".$key."%");
		//根据级别查找竞赛
		if($level)$where['level']=$level;
		//根据内容类别查找竞赛
		if($type)$where['content_type']=$type;

		$all=M($this->dbName)->join('__SCHOOL_ORG__ ON __COMPETITION__.school_id = __SCHOOL_ORG__.id')->where($where)->order('cpt_id desc')->field('cpt_id,title,tswc_competition.desc,logo,start_time,end_time,school,is_sign_start')->select();
		$data=R('Myfunc/Myfunc/pageDeal',array('data'=>$all));

		$data['key']=$key;
		$data['lvl']=$level;
		$data['type']=$type;

		echo json_encode($data);
	}
	// 竞赛详情
	public function detail(){
		tag('func');
		$id=I('get.id');
		if(!$id)$id=1;
		$data['competition']=M($this->dbName)->join('__SCHOOL_ORG__ ON __COMPETITION__.school_id = __SCHOOL_ORG__.id')->where('tswc_competition.cpt_id ='.$id.' and tswc_competition.is_verify = 1')->find();
		$find=M($this->dbName)->where('cpt_id = '.$id)->find();
		//附件
		$data['competition']['file']=$find['file'];
		//html转义
		$data['competition']['contain']=htmlspecialchars_decode($data['competition']['contain']);

		if($_ENV['user']['type']==1){
			if(M($this->dbName2)->where('std_id='.$_ENV['user']['id'].' and cpt_id ='.$id)->find())$data['is_join']=1;
			else $data['is_join']=0;
		}

		$this->assign('data',$data);
		// dump($data);
        R('Page/cptDetail');
	}
	// 竞赛结果页面
	public function result(){
		tag('func');
		$where['cpt_id']=I('get.id');
		$data['competition']=M($this->dbName)->join('__SCHOOL_ORG__ ON __COMPETITION__.school_id = __SCHOOL_ORG__.id')->where($where)->find();
		$data['competition_result']=M($this->dbName3)->where($where)->find();

		//html转义
		$data['competition_result']['cpt_result']=htmlspecialchars_decode($data['competition_result']['cpt_result']);

		$this->assign('data',$data);
		// dump($data);
        R('Page/cptResult');
	}
	// 竞赛结果列表接口
	public function resultAjax(){
		tag('func');
		$all=M('competition_result')->join('__COMPETITION__ ON __COMPETITION_RESULT__.cpt_id = __COMPETITION__.cpt_id')->join('__SCHOOL_ORG__ ON __COMPETITION__.school_id = __SCHOOL_ORG__.id')->order('tswc_competition_result.create_time desc')->field('tswc_competition_result.id,tswc_competition_result.cpt_id,logo,title,start_time,end_time,school,cpt_result')->select();
		$data=R('Myfunc/Myfunc/pageDeal',array('data'=>$all));
		// dump($data);
		echo json_encode($data);
	}
	// 报名页面
	public function joinPage(){
		tag('func2');
		$cpt_id=I('get.cpt_id');
		if(M($this->dbName2)->where('std_id='.$_ENV['user']['id'].' and cpt_id ='.$cpt_id)->find())$data['is_join']=1;
		else $data['is_join']=0;
		$this->assign('data',$data);
		// dump($data);
		R('Page/joinPage');
	}
	// 竞赛报名
	public function join(){
		tag('func2');
		$data['std_id']=$_ENV['user']['id'];
		$data['cpt_id']=I('post.cpt_id');
		$data['join_time']=date('Y-m-d H:i:s',time());

		// 报名附件
		$file=R('Myfunc/Myfunc/upload');
		if($file)$data['join_file']=$file;

		if(M($this->dbName2)->where('std_id='.$data['std_id'].' and cpt_id ='.$data['cpt_id'])->find())$result=array('code'=>2,'msg'=>'已经报过名');
		else if(M($this->dbName2)->add($data))$result=array('code'=>0,'msg'=>'报名提交成功');
		else $result=array('code'=>1,'msg'=>'报名提交失败');
		echo json_encode($result);
	}
	// 该用户参加的比赛
	public function myJoinList(){
		tag('func2');
		// $type=I('get.type')?I('get.type'):0;//0待审核，1参加过，2获奖
		$where['std_id']=$_ENV['user']['id'];
		$where['is_verify']=1;
		// if($type==0||$type==1)$where['join_status']=$type;
		if($type==2)$where['is_win']=1;
		$all=M($this->dbName2)->join('__COMPETITION__ ON __CPT_SIGN__.cpt_id=__COMPETITION__.cpt_id')->where($where)->order('join_time desc')->select();
		$data=R('Myfunc/Myfunc/pageDeal',array('data'=>$all));
		$data['type']=$type;
		$this->assign('data',$data);
		R('Page/myJoinList');
		// dump($data);
	}
	// 该用户参加的比赛ajax接口
	public function myJoin(){
		tag('func2');
		$where['std_id']=$_ENV['user']['id'];
		$where['join_status']=1;
		$all=M($this->dbName2)->join('__COMPETITION__ ON __CPT_SIGN__.cpt_id=__COMPETITION__.cpt_id')->where($where)->select();
		$data=R('Myfunc/Myfunc/pageDeal',array('data'=>$all));
		echo json_encode($data);
	}
	// 取消报名
	public function joinCancel(){
		tag('func2');
		$where['join_id']=I('post.join_id');
		$where['std_id']=$_ENV['user']['id'];
		if(M('cpt_sign')->where($where)->delete())$result=array('code'=>0,'msg'=>'取消成功');
		else $result=array('code'=>1,'msg'=>'取消失败');
		echo json_encode($result);
	}
}